package com.evil.common.application.enums.permission;

import cn.hutool.core.collection.ListUtil;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 权限可见状态
 *
 * @author liyang
 */
@AllArgsConstructor
public enum PermissionVisibleEnum {

    /**
     * 权限可见状态 (0:可使用, 1:仅可见, 2:不可见)
     **/
    AVAILABLE(0, "可使用"),
    VISIBLE(1, "仅可见"),
    INVISIBLE(2, "不可见"),
    ;
    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<PermissionVisibleEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static PermissionVisibleEnum isExistById(Integer id) {
        return PermissionVisibleEnum.findById(id).orElseThrow(()
                -> new BusinessException(RCodeEnum.ERROR_PARAMETER, "权限可见状态参数错误"));
    }

    /**
     * 获取可见 状态
     *
     * @return List
     */
    public static List<Integer> visible() {
        return ListUtil.toList(AVAILABLE.id, VISIBLE.id);
    }

    /**
     * 是否可见
     *
     * @return List
     */
    public static boolean isVisible(Integer id) {
        if (Objects.isNull(id)) {
            return false;
        }
        return PermissionVisibleEnum.visible().contains(id);
    }

    /**
     * 是否可使用
     *
     * @return List
     */
    public static boolean isAvailable(Integer id) {
        if (Objects.isNull(id)) {
            return false;
        }
        return AVAILABLE.id == id;
    }

    public static int getSpecialControlVisible(boolean visible) {
        return visible ? VISIBLE.id : INVISIBLE.id;
    }
}
